import numpy as np
from matplotlib import pyplot as plt
from sklearn.metrics import classification_report
from vis_function import *

def create_model():
    # 导入鸢尾花数据
    from sklearn.datasets import load_iris  # sklearn自带数据
    global iris_data
    iris_data = load_iris()
    print(type(iris_data))
    # data里面是[0]花萼长度、[1]花萼宽度、[2]花瓣长度、[3]花瓣宽度 的测量数据
    print(iris_data['data'])  # 花的样本数据
    print("花的样本数量：{}".format(iris_data['data'].shape))
    print("花的前5个样本数据：/n{}".format(iris_data['data'][:5]))

    # 0 代表 setosa， 1 代表 versicolor，2 代表 virginica
    print('target')# 类别
    print(iris_data['target'])
    print("花的类别数量：{}".format(iris_data['target'].shape))
    print('target_names')
    print(iris_data['target_names'])  # 花的品种

    # 载入数据集

    # 获取花卉两列数据集
    data = iris_data.data
    X = [x[0] for x in data]
    Y = [x[1] for x in data]
    # plt.scatter(X, Y, c=iris_data.target, marker='x')
    plt.scatter(X[:50], Y[:50], color='red', marker='o', label='setosa')  # 前50个样本
    plt.scatter(X[50:100], Y[50:100], color='blue', marker='x', label='versicolor')  # 中间50个
    plt.scatter(X[100:], Y[100:], color='green', marker='+', label='Virginica')  # 后50个样本
    plt.legend(loc=2)  # 左上角
    plt.show()

    # 数据划分
    from sklearn.model_selection import train_test_split
    global x_train, x_test, y_train, y_test
    x_train, x_test, y_train, y_test = train_test_split(iris_data['data'], iris_data['target'], test_size=0.5)

    # 创建模型
    name = input('Please enter the classification method:1 LogisticRegression or 2 SVM or 3 LDA\n')
    clf = get_clsFunc(name)  # 选取分类器
    global model
    model = clf.fit(x_train, y_train)  # 训练分类器
    model.coef_  # 特征系数
    print("特征系数：", model.coef_)
    model.intercept_  # 截距
    print("截距：", model.intercept_)


# 模型预测
def predict_model():
    print("模型预测结果：")
    global y_predict
    y_predict = model.predict(x_test)
    print(model.predict(x_test))  # 数据的类别
    print(model.predict_proba(x_test))  # 数据的概率


# 精度报告
def clf_report():
    cla_name = ['0', '1', '2']
    print('精度报告：')

    print(classification_report(y_test, y_predict, target_names=cla_name))


if __name__ == "__main__":
    create_model()
    predict_model()
    clf_report()
